import {
    PRODUCT_LIST,
    PRODUCT_LIST_OUTSALE,
    PRODUCT_CATEGORY,
    PRODUCT_CATEGORY_CHOOSE,
    PRODUCT_LIST_INCHECK,
    PRODUCT_BRAND_CHOOSE,
    PRODUCT_LIST_INEDIT,
    Property_Set,
    Property_Refresh,
    Property_Save,
    PRODUCT_BRAND, Product_Proprty
} from './constants';

const initState = {
    productOnSale: {
        list: [],
        page: 1
    },
    productOutSale: {
        list: [],
        page: 1
    },
    productInCheck: {
        list: [],
        page: 1
    },
    productInEdit:{
        list:[],
        page:1,
    },
    productCategorys: [],
    productBrands: [],
    chooseCategory: {},
    chooseBrand: '',

    /**
     * {"attrId":2,"attrName":"颜色","attrValue":"红色;白色","isMain":1,"isNormal":0}
     */
    propertyList: [],//所有属性
    propertyList_Nor: [],//常规属性
    propertyList_Standard: [],//规格属性
    Products: []//保存规格返回的数据
};

const reducer = (state = initState, action) => {
    switch (action.type) {
        case PRODUCT_LIST:
            if (action.payload.productOnSale.page == 1) {
                return Object.assign({}, state, action.payload);
            } else {
                let list = state.productOnSale.list;
                let newlist = list.concat(action.payload.productOnSale.list);
                return Object.assign({}, state, {
                    productOnSale: {
                        list: newlist,
                        page: action.payload.productOnSale.page
                    }
                })
            }
        case PRODUCT_LIST_OUTSALE:
            if (action.payload.productOutSale.page == 1) {
                return Object.assign({}, state, action.payload);
            } else {
                let list = state.productOutSale.list;
                let newlist = list.concat(action.payload.productOutSale.list);
                return Object.assign({}, state, {
                    productOutSale: {
                        list: newlist,
                        page: action.payload.productOutSale.page
                    }
                })
            }
        case PRODUCT_LIST_INEDIT:
            if (action.payload.productInEdit.page == 1) {
                return Object.assign({}, state, action.payload);
            } else {
                let list = state.productInEdit.list;
                let newlist = list.concat(action.payload.productInEdit.list);
                return Object.assign({}, state, {
                    productInEdit: {
                        list: newlist,
                        page: action.payload.productInEdit.page
                    }
                })
            }   
        case PRODUCT_LIST_INCHECK:
            if (action.payload.productInCheck.page == 1) {
                return Object.assign({}, state, action.payload);
            } else {
                let list = state.productInCheck.list;
                let newlist = list.concat(action.payload.productInCheck.list);
                return Object.assign({}, state, {
                    productInCheck: {
                        list: newlist,
                        page: action.payload.productInCheck.page
                    }
                })
            }
        case PRODUCT_CATEGORY:
            return Object.assign({}, state, action.payload);
        case PRODUCT_CATEGORY_CHOOSE:
            return Object.assign({}, state, action.payload);
        case PRODUCT_BRAND_CHOOSE:
            return Object.assign({}, state, action.payload);
        case PRODUCT_BRAND:
            return Object.assign({}, state, action.payload);
        case Product_Proprty:
            return Object.assign({}, state, action.payload);
        case Property_Set:
            let list = state.propertyList;
            let one = action.payload.propertyItem;
            list.map((item) => {
                if (item.attrId == one.attrId) {
                    list.attrValue = one.attrValue;
                }
            });
            return Object.assign({}, state, {propertyList: list});
        case Property_Refresh:
            let templist = state.propertyList;
            let norlist = [];
            let standardlist = [];
            templist.map((item) => {
                if (item.isDefault == 1) {
                    if (item.isNormal == 0) {
                        standardlist.push(item);
                    } else {
                        norlist.push(item);
                    }
                }
            });
            return Object.assign({}, state, {propertyList_Nor: norlist, propertyList_Standard: standardlist});
        case Property_Save:
            return Object.assign({}, state, action.payload);
        default:
            return state;
    }
};

export default reducer;
